package helloworld.admin.service.course;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import helloworld.dao.CourseDao;
import helloworld.dao.DAOException;
import helloworld.dao.DAOFactory;
import helloworld.example.exception.ErrorLevel;
import helloworld.exception.DuplicateCourseException;

import helloworld.admin.bean.*;

/** 
 * @author TUYEN-DM
 *         <p>
 *         2009/05/09
 *         </p>
 *         <p>
 *         In real projects, CustomerControllerService object is often implemented as Session EJB.
 * It represents a Business Service and is Session Facade pattern (Core J2EE Patterns) 
 *         </p>
 */
public class CourseControllerService {

	public CourseAdminBean getCourseDetail(String courseName) throws CourseServiceException
	{
		CourseAdminBean course = null;
		try{
			DAOFactory factory = DAOFactory.getDAOFactory(DAOFactory.MS_ACCESS);
			CourseDao dao = factory.getCourseDAO();
			course = dao.fetchDetail(courseName);
			
		}catch (DAOException de)
		{
			if (! de.isLogged())
			{
				Log log = LogFactory.getLog("CourseFetchService");
				de.log(log);
			}
			throw new CourseServiceException(de.getUserMessageKey(), 
									de.getValueReplacementArray(),
									de.getUniqueID(), 
									ErrorLevel.ERROR);
		}
		
		return course;
	}
	
	public int createCourse(CourseAdminBean course) throws CourseServiceException, DuplicateCourseException{
		try{
			/*course.getAddress().setAddress1("N/A");
			course.getAddress().setAddress2("N/A");*/
			DAOFactory factory = DAOFactory.getDAOFactory(DAOFactory.MS_ACCESS);
			CourseDao dao = factory.getCourseDAO();
			return dao.create(course);
		}catch (DAOException de) {
			// TODO: handle exception
			if(!de.isLogged()){
				Log log = LogFactory.getLog("CourseInsertService");
				de.log(log);
			}
			throw new CourseServiceException(de.getUserMessageKey(),de.getValueReplacementArray(),
												de.getUniqueID(),ErrorLevel.ERROR);
		}
	}
	
	public void updateCourse(CourseAdminBean course)throws CourseServiceException,DuplicateCourseException{
		try{
			/*course.getAddress().setAddress1("N/A");
			course.getAddress().setAddress2("N/A");*/
			DAOFactory factory = DAOFactory.getDAOFactory(DAOFactory.MS_ACCESS);
			CourseDao dao = factory.getCourseDAO();
			dao.update(course);
		}catch (DAOException de) {
			// TODO: handle exception
			if(!de.isLogged()){
				Log log = LogFactory.getLog("CourseUpdateService");
				de.log(log);
			}
			throw new CourseServiceException(de.getUserMessageKey(),de.getValueReplacementArray(),
												de.getUniqueID(),ErrorLevel.ERROR);
		}
	}
	
	public void deleteCourse(int id) throws CourseServiceException{
		try{
			
			DAOFactory factory = DAOFactory.getDAOFactory(DAOFactory.MS_ACCESS);
			CourseDao dao = factory.getCourseDAO();
			dao.delete(id);
		}catch (DAOException de) {
			// TODO: handle exception
			if(!de.isLogged()){
				Log log = LogFactory.getLog("CourseDeleteService");
				de.log(log);
			}
			throw new CourseServiceException(de.getUserMessageKey(),de.getValueReplacementArray(),
												de.getUniqueID(),ErrorLevel.ERROR);
		}
	}
}
